package duyd.jpa.training.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

@SuppressWarnings("serial")
@Entity
@Table(name = "MY_ENTITY")
@NamedQueries({
		@NamedQuery(name = "myEntity.findAll", query = "select c from Contact c"),
		@NamedQuery(name = "myEntity.findAllWithDetail", query = "select distinct m from MyEntity m left join fetch m.entitySetOne o left join fetch m.entitySetTwo t") })
public class MyEntity implements Serializable {
	private Long id;
	private String name;
	private Date createDate;
	private Date lastUpdate;
	private Character deleted;

	private Set<MyEntity> entitySetOne = new HashSet<MyEntity>();
	private Set<MyEntity> entitySetTwo = new HashSet<MyEntity>();

	public MyEntity() {

	}

	public MyEntity(String name, Date createDate, Date lastUpdate,
			Character deleted, Set<MyEntity> entitySetOne,
			Set<MyEntity> entitySetTwo) {
		super();
		this.name = name;
		this.createDate = createDate;
		this.lastUpdate = lastUpdate;
		this.deleted = deleted;
		this.entitySetOne = entitySetOne;
		this.entitySetTwo = entitySetTwo;
	}

	public MyEntity(String name, Date createDate, Date lastUpdate,
			Character deleted) {
		super();
		this.name = name;
		this.createDate = createDate;
		this.lastUpdate = lastUpdate;
		this.deleted = deleted;
	}

	@Id
	@GeneratedValue(generator = "increment")
	@GenericGenerator(name = "increment", strategy = "increment")
	@Column(name = "MY_ENTITY_ID")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "NAME")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "CREATE_DATE")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Column(name = "LAST_UPDATE")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	@Column(name = "DELETED")
	public Character getDeleted() {
		return deleted;
	}

	public void setDeleted(Character deleted) {
		this.deleted = deleted;
	}

	@ManyToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "MY_ENTITY_RELATION", joinColumns = @JoinColumn(name = "ENTITY_ONE_ID", referencedColumnName = "MY_ENTITY_ID"), inverseJoinColumns = @JoinColumn(name = "ENTITY_TWO_ID", referencedColumnName = "MY_ENTITY_ID"))
	public Set<MyEntity> getEntitySetOne() {
		return entitySetOne;
	}

	public void setEntitySetOne(Set<MyEntity> entitySetOne) {
		this.entitySetOne = entitySetOne;
	}

	@ManyToMany(mappedBy = "entitySetOne")
	public Set<MyEntity> getEntitySetTwo() {
		return entitySetTwo;
	}

	public void setEntitySetTwo(Set<MyEntity> entitySetTwo) {
		this.entitySetTwo = entitySetTwo;
	}
	
	public String toString() {
		return name;
	}

}
